package com.jxinternet.platform.services.service.impl;

import com.google.protobuf.ByteString;
import com.google.protobuf.MessageLite;
import com.jxinternet.platform.services.coder.MessageDecoder;
import com.jxinternet.platform.services.coder.MessageEncoder;
import com.jxinternet.platform.services.entity.protobuf.BattleProtos.BattleRequest;
import com.jxinternet.platform.services.entity.protobuf.BattleProtos.BattleResponse;
import com.jxinternet.platform.services.entity.protobuf.LoginProtos;
import com.jxinternet.platform.services.entity.protobuf.LoginProtos.LoginRequest;
import com.jxinternet.platform.services.entity.protobuf.LoginProtos.LoginResponse;
import com.jxinternet.platform.services.entity.protobuf.RegisterProtos.RegisterRequest;
import com.jxinternet.platform.services.entity.protobuf.RegisterProtos.RegisterResponse;
import com.jxinternet.platform.services.entity.protobuf.SocketModelProtos.SocketModelRequest;
import com.jxinternet.platform.services.entity.protobuf.SocketModelProtos.SocketModelResponse;
import com.jxinternet.platform.services.entity.protobuf.WealthProtos.WealthResponse;
import com.jxinternet.platform.services.protocol.BaseProtocol;
import com.jxinternet.platform.services.service.DispatchService;
import com.jxinternet.platform.services.service.GameService;
import com.jxinternet.platform.services.service.LoginService;
import com.jxinternet.platform.services.service.RegisterService;
import com.jxinternet.platform.services.service.UserInfoService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by gold-jx on 2017/7/19.
 */
@Slf4j
@Component
public class DispatchServiceImpl implements DispatchService {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RegisterService registerService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private GameService gameService;


    @Override
    public SocketModelResponse loginDispatch(ChannelHandlerContext ctx, SocketModelRequest message) {

        SocketModelResponse.Builder builder = SocketModelResponse.newBuilder();
        builder.setMessageId(message.getMessageId());
        builder.setCmd(message.getCmd());
        ByteString byteString = ByteString.copyFromUtf8(message.getMessageId());
        builder.setData(byteString);
        Channel incoming = ctx.channel();
        try {
            //log.info("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "开始处理登录请求的事务");
            MessageDecoder messageDecoder = new MessageDecoder();
            MessageLite messageLite = messageDecoder.toDecode(message);
            LoginProtos.LoginRequest loginRequest = (LoginRequest) messageLite;
            //处理登录请求的事务
            LoginResponse loginResponse = loginService.Login(loginRequest, incoming.remoteAddress().toString());
            MessageEncoder messageEncoder = new MessageEncoder();
            ByteString data = messageEncoder.toEncode(loginResponse, BaseProtocol.Area_Login);
            builder.setResultStatus(BaseProtocol.SocketModel_Invalid_Success_Code);
            builder.setResultDescription(BaseProtocol.SocketModel_Invalid_Success_Res);
            builder.setData(data);
            //log.info("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "结束处理登录请求的事务");
            return builder.build();

        } catch (Exception e) {
            builder.setResultStatus(BaseProtocol.SocketModel_Invalid_System_Error_Code);
            builder.setResultDescription(BaseProtocol.SocketModel_Invalid_System_Error_Res);
            //log.error("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "处理事务异常");
            return builder.build();
        }
    }

    @Override
    public SocketModelResponse registerDispatch(ChannelHandlerContext ctx, SocketModelRequest message) {
        SocketModelResponse.Builder builder = SocketModelResponse.newBuilder();
        builder.setMessageId(message.getMessageId());
        builder.setCmd(message.getCmd());
        ByteString byteString = ByteString.copyFromUtf8(message.getMessageId());
        builder.setData(byteString);
        Channel incoming = ctx.channel();

        try {
            //log.info("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "开始处理注册请求的事务");
            MessageDecoder messageDecoder = new MessageDecoder();
            MessageLite messageLite = messageDecoder.toDecode(message);
            RegisterRequest registerRequest = (RegisterRequest) messageLite;
            System.out.println(registerRequest.getNickName());
            //处理注册的事务
            RegisterResponse registerResponse = registerService.register(registerRequest, incoming.remoteAddress().toString());
            MessageEncoder messageEncoder = new MessageEncoder();
            ByteString data = messageEncoder.toEncode(registerResponse, BaseProtocol.Area_User_Register);
            builder.setResultStatus(BaseProtocol.SocketModel_Invalid_Success_Code);
            builder.setResultDescription(BaseProtocol.SocketModel_Invalid_Success_Res);
            builder.setData(data);
            //log.info("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "结束处理注册请求的事务");
            return builder.build();

        } catch (Exception e) {
            builder.setResultStatus(BaseProtocol.SocketModel_Invalid_System_Error_Code);
            builder.setResultDescription(BaseProtocol.SocketModel_Invalid_System_Error_Res);
            //log.error("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "处理事务异常");
            return builder.build();
        }

    }

    @Override
    public SocketModelResponse userInfoDispatch(ChannelHandlerContext ctx, SocketModelRequest message) {
        SocketModelResponse.Builder builder = SocketModelResponse.newBuilder();
        builder.setMessageId(message.getMessageId());
        builder.setCmd(message.getCmd());
        ByteString byteString = ByteString.copyFromUtf8(message.getMessageId());
        builder.setData(byteString);
        Channel incoming = ctx.channel();

        try {
            //log.info("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "开始处理加载财富请求的事务");
            //处理初始化财富事务
            WealthResponse wealthResponse = userInfoService.initUserInfo(message.getUserName(), incoming.remoteAddress().toString());
            MessageEncoder messageEncoder1 = new MessageEncoder();
            ByteString data1 = messageEncoder1.toEncode(wealthResponse, BaseProtocol.Area_User_LoadWealth);
            builder.setResultStatus(BaseProtocol.SocketModel_Invalid_Success_Code);
            builder.setResultDescription(BaseProtocol.SocketModel_Invalid_Success_Res);
            builder.setData(data1);
            //log.info("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "结束处理加载财富请求的事务");
            return builder.build();

        } catch (Exception e) {
            builder.setResultStatus(BaseProtocol.SocketModel_Invalid_System_Error_Code);
            builder.setResultDescription(BaseProtocol.SocketModel_Invalid_System_Error_Res);
            //log.error("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "处理事务异常");
            return builder.build();
        }
    }

    @Override
    public SocketModelResponse BattleDispatch(ChannelHandlerContext ctx, SocketModelRequest message) {
        SocketModelResponse.Builder builder = SocketModelResponse.newBuilder();
        builder.setMessageId(message.getMessageId());
        builder.setCmd(message.getCmd());
        ByteString byteString = ByteString.copyFromUtf8(message.getMessageId());
        builder.setData(byteString);

        try {
            switch (message.getCmd()) {
                case BaseProtocol.Area_Battle://作战
                    //log.info("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "开始处理作战的事务");
                    MessageDecoder messageDecoder = new MessageDecoder();
                    MessageLite messageLite = messageDecoder.toDecode(message);
                    BattleRequest battleRequest = (BattleRequest) messageLite;
                    //处理作战的事务
                    BattleResponse battleResponse = gameService.battle(battleRequest, message.getUserName());
                    MessageEncoder messageEncoder = new MessageEncoder();
                    ByteString data = messageEncoder.toEncode(battleResponse, BaseProtocol.Area_Battle);
                    builder.setResultStatus(BaseProtocol.SocketModel_Invalid_Success_Code);
                    builder.setResultDescription(BaseProtocol.SocketModel_Invalid_Success_Res);
                    builder.setData(data);
                    //log.info("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "结束处理作战的事务");
                    return builder.build();
                default:
                    //log.error("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "请求接口不存在");
                    builder.setResultStatus(BaseProtocol.SocketModel_Invalid_Interface_Not_Exist_Code);
                    builder.setResultDescription(BaseProtocol.SocketModel_Invalid_Interface_Not_Exist_Res);
                    return builder.build();
            }

        } catch (Exception e) {
            builder.setResultStatus(BaseProtocol.SocketModel_Invalid_System_Error_Code);
            builder.setResultDescription(BaseProtocol.SocketModel_Invalid_System_Error_Res);
            //log.error("消息编号为:" + message.getMessageId() + " 接口编号为:" + message.getCmd() + " 用户为:" + message.getUserName() + "处理事务异常");
            return builder.build();
        }
    }
}
